package appx.craft.droid.ui;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import appx.craft.droid.R;
import appx.craft.droid.bean.CommentBeans;
import appx.craft.droid.uc.CommentsAdapters;
import appx.craft.droid.utils.Log;

/**
 * 
 * This class is help for show user comment from feed listing screen once user press on comment text 
 *
 */
public class StreamCommentActivity extends FragmentActivity implements OnItemClickListener{
	
	private final String TAG = StreamCommentActivity.class.getCanonicalName();
	
	private ListView listview_comments;
	private CommentsAdapters mCommentsAdapter;
	private ArrayList<CommentBeans> mArrayCommentsList;
	private CommentBeans mTempBeans;
	private Button btn_send;
	private EditText editText_comments;
	@Override
	protected void onCreate(Bundle saveInstance) {
		super.onCreate(saveInstance);
		setContentView(R.layout.activity_stream_comments);
		Log.d(TAG, "[ onCreate ]");
		
		editText_comments = (EditText)findViewById(R.id.editText_comments);
		editText_comments.setOnKeyListener(new OnKeyListener() 
		{
             @Override
             public boolean onKey(View v, int keyCode, KeyEvent event) {
            	 if (keyCode == KeyEvent.KEYCODE_ENTER) 
            	 {
		             int lineCount = ((EditText)v).getText().toString().split("\\n").length;
		             if (lineCount > 3) {
		                     return false;
	             		}
                 }
                 return false;
             }
		});
		
		listview_comments = (ListView)findViewById(R.id.listview_comments);
		listview_comments.setOnItemClickListener(this);
		
		mCommentsAdapter = new CommentsAdapters(StreamCommentActivity.this);
		listview_comments.setAdapter(mCommentsAdapter);
		
		mArrayCommentsList = new ArrayList<CommentBeans>();
		
		/** Here is static item in array list you have to add your data item here **/
		
		for(int i=0;i<5;i++){
			mTempBeans = new CommentBeans();
			mTempBeans.setStrCaptions("Nice");
			mTempBeans.setStrName("ABC Test");
			mArrayCommentsList.add(mTempBeans);	
		}
		
		new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					
					mCommentsAdapter.addNewCommentsLists(mArrayCommentsList);
					mCommentsAdapter.loadMoreView(true);
					listview_comments.post(new Runnable() {
						@SuppressLint("NewApi")
						public void run() {
							listview_comments.setSelection(mArrayCommentsList.size()-1);
							listview_comments.smoothScrollToPosition(mArrayCommentsList.size());
						}
					});
				}
			},1000);
		 
		 btn_send = (Button)findViewById(R.id.btn_send);
		 btn_send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				 new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							
							if(editText_comments.getText().toString().length() > 0 && !editText_comments.getText().toString().equalsIgnoreCase("")){
								
								CommentBeans mNewAddBeans = new CommentBeans();
								mNewAddBeans.setStrName("ABC Test");
								mNewAddBeans.setStrCaptions(editText_comments.getText().toString());
								mCommentsAdapter.addNewComments(mNewAddBeans);
								// if you know that you have more page still then we have show view to user
								mCommentsAdapter.loadMoreView(true);
								editText_comments.setText(null);
								listview_comments.post(new Runnable(){
									  public void run() {
										  listview_comments.setSelection(listview_comments.getCount() - 1);
									  }});

							}else{
								Toast.makeText(getApplicationContext(),"Please enter comment", Toast.LENGTH_SHORT).show();
							}
						}
					},700);
				
			}
		});

	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "[ onStart ]");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "[ onResume ]");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "[ onPause ]");
	}
	
	
	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(TAG, "[ onRestart ]");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "[ onStop ]");
	}
	
	/**
	 * Any comment click will call below call back for move on Profile activity
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		startActivity(new Intent(StreamCommentActivity.this,ProfileActivity.class));
	}
	
}
